import java.util.Scanner;

public class demo05 {
    //小朋友崇拜圈
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int[] arr=new int[n+1];
        for (int i = 1; i < n+1; i++) {
            arr[i]=sc.nextInt();
        }
        int max=0;
        for (int i = 1; i < n+1; i++) {
            int count=dfs(arr,i,0);
            if(count>max){
                max=count;
            }
        }
        System.out.println(max);
    }
    public static int dfs(int[] arr,int i,int count){
        int num=arr[i];
        count++;
        while (num!=i){
            if(count>arr.length){
                return 0;
            }
            num=arr[num];
            count++;
        }
        return count;
    }
}
